package com.suao.goods.service.impl;

import com.suao.common.annotation.DataScope;
import com.suao.common.constant.DelFlagConst;
import com.suao.common.utils.DateUtils;
import com.suao.common.utils.SecurityUtils;
import com.suao.common.utils.StringUtils;
import com.suao.goods.domain.GoodsInfo;
import com.suao.goods.domain.GoodsSalesInfo;
import com.suao.goods.mapper.GoodsSalesInfoMapper;
import com.suao.goods.service.IGoodsInfoService;
import com.suao.goods.service.IGoodsSalesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商品销售流水Service业务层处理
 *
 * @author tyz
 * @date 2023-02-27
 */
@Service
public class GoodsSalesInfoServiceImpl implements IGoodsSalesInfoService
{
    @Autowired
    private GoodsSalesInfoMapper goodsSalesInfoMapper;
    
    
    @Autowired
    private IGoodsInfoService goodsInfoService;

    /**
     * 查询商品销售流水
     *
     * @param id 商品销售流水主键
     * @return 商品销售流水
     */
    @Override
    public GoodsSalesInfo selectGoodsSalesInfoById(Long id)
    {
        return goodsSalesInfoMapper.selectGoodsSalesInfoById(id);
    }

    /**
     * 查询商品销售流水列表
     *
     * @param goodsSalesInfo 商品销售流水
     * @return 商品销售流水
     */
    @Override
    @DataScope(userAlias = "u")
    public List<GoodsSalesInfo> selectGoodsSalesInfoList(GoodsSalesInfo goodsSalesInfo)
    {
        return goodsSalesInfoMapper.selectGoodsSalesInfoList(goodsSalesInfo);
    }

    /**
     * 新增商品销售流水
     *
     * @param goodsSalesInfo 商品销售流水
     * @return 结果
     */
    @Override
    public int insertGoodsSalesInfo(GoodsSalesInfo goodsSalesInfo)
    {
        goodsSalesInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        goodsSalesInfo.setCreateTime(DateUtils.getNowDate());
        goodsSalesInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        goodsSalesInfo.setUpdateTime(DateUtils.getNowDate());
        int result = goodsSalesInfoMapper.insertGoodsSalesInfo(goodsSalesInfo);
        if(result>0)
        {
            //减掉库存
            GoodsInfo temp = goodsInfoService.selectGoodsInfoById(goodsSalesInfo.getGoodsId());
            if(StringUtils.isNotNull(temp))
            {
                BigDecimal inventory = temp.getInventory().subtract(goodsSalesInfo.getSalesNum());
                temp.setInventory(inventory);
            }
            goodsInfoService.updateGoodsInfo(temp);
        }
        return result;
        
    }

    /**
     * 修改商品销售流水
     *
     * @param goodsSalesInfo 商品销售流水
     * @return 结果
     */
    @Override
    public int updateGoodsSalesInfo(GoodsSalesInfo goodsSalesInfo)
    {
        //1.获取原始销售数量
        GoodsSalesInfo old = goodsSalesInfoMapper.selectGoodsSalesInfoById(goodsSalesInfo.getId());
       BigDecimal oldSalesNum = old.getSalesNum();
        goodsSalesInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        goodsSalesInfo.setUpdateTime(DateUtils.getNowDate());
       int result = goodsSalesInfoMapper.updateGoodsSalesInfo(goodsSalesInfo);
       if(result>0)
       {
           //减掉库存
           GoodsInfo temp = goodsInfoService.selectGoodsInfoById(goodsSalesInfo.getGoodsId());
           if(StringUtils.isNotNull(temp))
           {
               //先把库存加回去再减掉最新的销售数量
               BigDecimal inventory = temp.getInventory().add(oldSalesNum).subtract(goodsSalesInfo.getSalesNum());
               temp.setInventory(inventory);
           }
           goodsInfoService.updateGoodsInfo(temp);
       }
       return result;
    }

    /**
     * 批量删除商品销售流水
     *
     * @param ids 需要删除的商品销售流水主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSalesInfoByIds(Long[] ids)
    {
        int count =0;
        for(Long id:ids)
        {
            updateGoodsSalesInfoDelFlag(id);
            count++;
        }
        return count;
    }

    /**
     * 删除商品销售流水信息
     *
     * @param id 商品销售流水主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSalesInfoById(Long id)
    {
        updateGoodsSalesInfoDelFlag(id);
        return 1;
    }
    
    public void  updateGoodsSalesInfoDelFlag(Long id)
    {
        GoodsSalesInfo temp = goodsSalesInfoMapper.selectGoodsSalesInfoById(id);
        temp.setDelFlag(DelFlagConst.DELETED);
        temp.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        temp.setUpdateTime(DateUtils.getNowDate());
        goodsSalesInfoMapper.updateGoodsSalesInfo(temp);
        
        //+回库存
        GoodsInfo goodsInfo = goodsInfoService.selectGoodsInfoById(temp.getGoodsId());
        if(StringUtils.isNotNull(goodsInfo))
        {
            //先把库存加回去再减掉最新的销售数量
            BigDecimal inventory = goodsInfo.getInventory().add(temp.getSalesNum());
            goodsInfo.setInventory(inventory);
        }
        goodsInfoService.updateGoodsInfo(goodsInfo);
    }
}
